from django.shortcuts import render_to_response
from django.template import RequestContext
from django.core.paginator import Paginator
from django.db.models import Q
from okuz.catalog.load import * 
from okuz.profile.models import *


ORDERS = {'asc':'last_name', 'desc':'-last_name'};
DEFAULT = {'sort':'asc', 'item':'25', 'page':'1'};

def archive (request):
	param = {'sort':request.GET.get ('sort', DEFAULT['sort']),
			'item':request.GET.get ('item', DEFAULT['item']),
			'page':request.GET.get ('page', DEFAULT['page']),
			'query':request.GET.get ('query', '') };
	if request.POST or param['query']:
		param['query'] = request.POST.get ('query', param['query']);
		archive = Profile.objects.filter (Q(first_name__icontains=param['query'])
										| Q(last_name__icontains=param['query'])
										| Q(personal_id__icontains=param['query'])).order_by (ORDERS[param['sort']]);
	else:
		archive = Profile.objects.all ().order_by (ORDERS[param['sort']]);
	paginator = Paginator (archive, param['item']);
	try:
		archivePage = paginator.page (param['page']);
	except PageNotAnInteger:
		archivePage = paginator.page (1);
	except EmptyPage:
		archivePage = paginator.page (paginator.num_pages);
	return render_to_response ('catalog/archive.html',
								{'archives':archivePage, 'param':param},
    							context_instance = RequestContext (request));


def load (request):
	message = [load_gender (), load_maritalstatus (), load_profile (), load_comment (), load_visit ()];
	msg = u"%s   %s   %s   %s	%s" % (message[0], message[1], message[2], message[3], message[4]);
	return render_to_response ('info.html', {'title':'Import danych', 'msg': msg},
								context_instance = RequestContext (request));
